package com.example.listviewtest;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * @author Yingyong Lao
 * 创建时间 2022/3/2 19:46
 * @version 1.0
 */
public class HeroAdapter extends BaseAdapter {
    private List<BaseItem> list;

    public HeroAdapter(List<BaseItem> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        int itemViewType = getItemViewType(i);
        if (view==null){
            if (itemViewType==BaseItem.TEXT){
                view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.text_item,viewGroup,false);
                TextViewHolder textViewHolder = new TextViewHolder();
                textViewHolder.groupNameTv=view.findViewById(R.id.tv);
                view.setTag(textViewHolder);
            }else if (itemViewType==BaseItem.HERO1){
                view=initView(viewGroup,R.layout.hero_item);
            }else if (itemViewType==BaseItem.HERO2){
                view=initView(viewGroup,R.layout.hero_item2);
            }else if (itemViewType==BaseItem.HERO3){
                view=initView(viewGroup,R.layout.hero_item3);
            }else if (itemViewType==BaseItem.HERO4){
                view=initView(viewGroup,R.layout.hero_item4);
            }else {
                view=new View(viewGroup.getContext());
            }
        }
        Object tag = view.getTag();
        BaseItem baseItem = list.get(i);
        if (tag instanceof TextViewHolder){
            TextViewHolder textViewHolder= (TextViewHolder) tag;
            if (baseItem instanceof TextItem){
                TextItem textItem= (TextItem) baseItem;
                textViewHolder.groupNameTv.setText(textItem.getGroupName());
            }
        }else if(tag instanceof HeroViewHolder){
            HeroViewHolder heroViewHolder= (HeroViewHolder) tag;
            if ((baseItem instanceof HeroItem)){
                HeroItem heroItem= (HeroItem) baseItem;
                heroViewHolder.nameTv.setText(heroItem.getHeroName());
                heroViewHolder.genderTv.setText(heroItem.getGender());
                heroViewHolder.wordTv.setText(heroItem.getWord());
            }
        }
        return view;
    }

    private View initView(ViewGroup viewGroup, int layout){
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(layout, viewGroup, false);
        HeroViewHolder heroViewHolder=new HeroViewHolder();
        heroViewHolder.nameTv= view.findViewById(R.id.nameTv);
        heroViewHolder.genderTv= view.findViewById(R.id.genderTv);
        heroViewHolder.wordTv= view.findViewById(R.id.wordTv);
        view.setTag(heroViewHolder);
        return view;
    }

    @Override
    public int getItemViewType(int position) {
        BaseItem baseItem = list.get(position);
        int type=baseItem.getViewType();
        if(BaseItem.TEXT==type){
            return BaseItem.TEXT;
        }else {
            if(position<list.size()-1){//不是最后一项
                BaseItem beforeItem = list.get(position - 1);
                BaseItem afterItem = list.get(position + 1);
                if (beforeItem.getViewType()==BaseItem.TEXT){
                    if (afterItem.getViewType()==BaseItem.TEXT){
                        return BaseItem.HERO1;//四个角都是圆的
                    }else {
                        return BaseItem.HERO2;//上面两个角是圆的
                    }
                }else {
                    if(afterItem.getViewType()==BaseItem.TEXT){
                        return BaseItem.HERO3;//下面两个角是圆的
                    }else {
                        return BaseItem.HERO4;//没有一个角是圆的
                    }

                }
            }else {//最后一项
                BaseItem beforeItem = list.get(position - 1);
                if (beforeItem.getViewType()==BaseItem.TEXT){
                    return BaseItem.HERO1;
                }else {
                    return BaseItem.HERO3;
                }
            }
        }
    }

    @Override
    public int getViewTypeCount() {
        return 6;
    }

    public static class BaseItem{
        public static final int TEXT=0;
        public static final int HERO=1;
        public static final int HERO1=2;//全圆角
        public static final int HERO2=3;//上半圆角
        public static final int HERO3=4;//下半圆角
        public static final int HERO4=5;//无圆角
        private int viewType;

        public BaseItem(int viewType) {
            this.viewType = viewType;
        }

        public int getViewType() {
            return viewType;
        }

        public void setViewType(int viewType) {
            this.viewType = viewType;
        }
    }

    static class TextViewHolder{
        TextView groupNameTv;
    }

    static class HeroViewHolder{
        TextView nameTv;
        TextView genderTv;
        TextView wordTv;
    }

    public static class TextItem extends BaseItem{
        private String groupName;


        public TextItem(int viewType, String groupName) {
            super(viewType);
            this.groupName = groupName;
        }

        public String getGroupName() {
            return groupName;
        }

        public void setGroupName(String groupName) {
            this.groupName = groupName;
        }
    }

    public static class HeroItem extends BaseItem{
        private String heroName;
        private String gender;
        private String word;

        public HeroItem(int viewType, String heroName, String gender, String word) {
            super(viewType);
            this.heroName = heroName;
            this.gender = gender;
            this.word = word;
        }

        public String getHeroName() {
            return heroName;
        }

        public void setHeroName(String heroName) {
            this.heroName = heroName;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public String getWord() {
            return word;
        }

        public void setWord(String word) {
            this.word = word;
        }
    }
}
